package easier.fast.base;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.MotionEvent;
import androidx.annotation.LayoutRes;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import easier.app.x.swipedismiss.SwipeDismiss;
import easier.app.x.view.SystemUi;
import java.util.concurrent.atomic.AtomicBoolean;

/**
 * 基础Activity,所有Activity继承自此
 */
public abstract class BaseActivity extends AppCompatActivity {

    public static final String TAG = "DialogProgress";

    private SwipeDismiss mSwipeDismiss;

    /**
     * 进度条
     */
    private ProgressDialog mProgressDialog;

    /**
     * 标记是否分发触摸事件,当activity延迟关闭时,禁用所有触摸事件
     */
    private final AtomicBoolean mDispatchTouchEvent = new AtomicBoolean( true );

    public Activity getActivity() {

        return this;
    }

    public Context getContext() {

        return this;
    }

    @Override
    protected void onCreate( @Nullable Bundle savedInstanceState ) {

        super.onCreate( savedInstanceState );

        if ( couldSwipeDismiss() ) {
            mSwipeDismiss = new SwipeDismiss( this );
            mSwipeDismiss.setCouldSwipeX( 200 );
        }

        SystemUi.transparentStatus( this );
        setContentView( getLayoutId() );
        initView();
        initViewAction();
    }

    public void showProgress() {

        if ( mProgressDialog == null ) {
            mProgressDialog = new ProgressDialog();
        }

        if ( !mProgressDialog.isShowing() ) {
            mProgressDialog.show( getSupportFragmentManager(), TAG );
        }
    }

    public void dismissProgress() {

        if ( mProgressDialog != null ) {
            mProgressDialog.dismiss();
        }
    }

    public boolean progressIsShowing() {

        if ( mProgressDialog != null ) {
            return mProgressDialog.isShowing();
        }

        return false;
    }

    /**
     * @return 设置布局Id, 创建界面
     */
    @LayoutRes
    protected abstract int getLayoutId();

    /**
     * findViewById
     */
    protected void initView() {

    }

    /**
     * 为View设置各种事件
     */
    protected void initViewAction() {

    }

    @Override
    protected void onStart() {

        super.onStart();
    }

    @Override
    protected void onResume() {

        super.onResume();
    }

    @Override
    protected void onPause() {

        super.onPause();
    }

    @Override
    protected void onStop() {

        super.onStop();
    }

    @Override
    protected void onDestroy() {

        super.onDestroy();
    }

    @Override
    public void onBackPressed() {

        super.onBackPressed();
    }

    /**
     * 是否允许分发触摸事件
     */
    public void setDispatchTouchEvent( boolean allow ) {

        mDispatchTouchEvent.set( allow );
    }

    @Override
    public boolean dispatchTouchEvent( MotionEvent ev ) {

        if ( mDispatchTouchEvent.get() ) {
            if ( couldSwipeDismiss() ) {
                mSwipeDismiss.onActivityTouchEvent( ev );
                boolean toMove = mSwipeDismiss.isToMove();
                if ( toMove ) {
                    return true;
                } else {
                    return super.dispatchTouchEvent( ev );
                }
            } else {
                return super.dispatchTouchEvent( ev );
            }
        } else {
            return true;
        }
    }

    /**
     * @return 配置是否可以
     */
    protected boolean couldSwipeDismiss() {

        return false;
    }
}
